//
//  MyOrderViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/22.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "MyOrderViewController.h"



#import "AllOrderModel.h"//全部订单Model

#import "OrderDetailViewController.h"//订单详情Controller

@interface MyOrderViewController ()<UIScrollViewDelegate>

@end

@implementation MyOrderViewController
{
    UIView *_slider;//滑动条
    
    UIScrollView *_scrollView;
    
    NSUserDefaults *_defaluts;
    
    NSMutableArray *_allOrderArray;
    NSMutableArray *_obligationArray;
    NSMutableArray *_committedArray;
    NSMutableArray *_terminalReceivingArray;
    NSMutableArray *_evaluationArray;
    
    float width;
    float height;
    
    NSMutableArray *_labelArray;
    NSMutableArray *_buttonArray;
    NSString *_userId;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //设置显示页面
    [self setSlider];
    
    
    _defaluts = [NSUserDefaults standardUserDefaults];
 
    if ([_defaluts objectForKey:@"Id"]) {
        _userId = [_defaluts objectForKey:@"Id"];
        //网络请求
        [self network:_index];
        for (int i = 0; i < 5; i++) {
            if (i == _index) {
                break;
            }
            [self network:i];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    width = SCREEN_WIDTH / 5;
    height = 40;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航栏
    [self setNav];
    
    //创建滑动View
    [self createSliderView];
    
    //设置ScrollView
    [self setScrollView];
    
    //设置TableView
    [self setTableView];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    self.view.backgroundColor = BackgroundCOLOR;
    self.title = @"我的订单";
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"返回";
    self.navigationItem.backBarButtonItem = back;
}

#pragma mark === 创建滑动View ===
- (void)createSliderView{
    
    NSArray *arr = @[@"全部", @"待付款", @"待发货", @"待收货", @"待评价"];
    _labelArray = [NSMutableArray array];
    _buttonArray = [NSMutableArray array];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, height)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    for (int i = 0; i < 5; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * width, 0, width, height);
        btn.tag = 11110 + i;
        [btn addTarget:self action:@selector(slider:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        UILabel *label = [[UILabel alloc] initWithFrame:btn.frame];
        label.text = arr[i];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = btn.tag + 10;
        [view addSubview:label];
        if (i == 0){
            label.textColor = APPCOLOR;
        }
        [_labelArray addObject:label];
        [_buttonArray addObject:btn];
    }
    _slider = [[UIView alloc] initWithFrame:CGRectMake(8, height - 3, width - 16, 3)];
    _slider.backgroundColor = APPCOLOR;
    [view addSubview:_slider];
    [view bringSubviewToFront:_slider];
}

- (void)slider:(UIButton *)sender{
    for (int i = 0; i < _labelArray.count; i++) {
        UILabel *label = _labelArray[i];
        UIButton *button = _buttonArray[i];
        if (button == sender) {
            label.textColor = APPCOLOR;
            [UIView animateWithDuration:0.2 animations:^{
                _slider.frame = CGRectMake( 8 + i * width, height - 3, width - 16, 3);
            }];
            
            _scrollView.contentOffset = CGPointMake(i * SCREEN_WIDTH, 0);
        }else label.textColor = [UIColor blackColor];
    }
}
#pragma mark === 设置ScrollView ===
- (void)setScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 107, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //实际尺寸
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 5, SCREEN_HEIGHT - 94);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        _slider.frame = CGRectMake(8 + scrollView.contentOffset.x / 5, height - 3, width - 16, 3);
        
        for (UILabel *label in _labelArray) {
            label.textColor = [UIColor blackColor];
        }
        //全部
        if (scrollView.contentOffset.x < SCREEN_WIDTH / 2) {
            UILabel *label = (id)[self.view viewWithTag:11110 + 10];
            label.textColor = APPCOLOR;
            if (_userId) {
                //网络请求
//                [self network:0];
            }
        //待付款
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH / 2&& scrollView.contentOffset.x < SCREEN_WIDTH * 3 / 2) {
            UILabel *label = (id)[self.view viewWithTag:11110 + 11];
            label.textColor = APPCOLOR;
            if (_userId) {
                //网络请求
//                [self network:1];
            }
        //待发货
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH * 3 / 2 && scrollView.contentOffset.x < SCREEN_WIDTH * 5 / 2){
            UILabel *label = (id)[self.view viewWithTag:11110 + 12];
            label.textColor = APPCOLOR;
            if (_userId) {
                //网络请求
//                [self network:2];
            }
        //待收货
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH * 5 / 2 && scrollView.contentOffset.x < SCREEN_WIDTH * 7 / 2){
            UILabel *label = (id)[self.view viewWithTag:11110 + 13];
            label.textColor = APPCOLOR;
            if (_userId) {
                //网络请求
//                [self network:3];
            }
        //待评价
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH * 7 / 2 && scrollView.contentOffset.x < SCREEN_WIDTH * 5){
            UILabel *label = (id)[self.view viewWithTag:11110 + 14];
            label.textColor = APPCOLOR;
            if (_userId) {
                //网络请求
//                [self network:4];
            }
        }
        
        if (scrollView.contentOffset.x < 0){
            _slider.frame = CGRectMake( 8, height - 3, width - 16, 3);
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH * 4){
            _slider.frame = CGRectMake(SCREEN_WIDTH * 4 / 5 + 8, height - 3, width - 16, 3);
        }
    }
}
#pragma mark === 设置TableView ===
- (void)setTableView{
    
    __weak __typeof__(self) weakSelf = self;
    _all = [[AllOrderTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 94)];
    
    _all.cellDidSelected = ^(NSString *status, NSInteger sindex, NSInteger rindex){
        AllOrderModel *model = [weakSelf.all.allOrderArray objectAtIndex:sindex][0][0];
        
        OrderDetailViewController *detail = [[OrderDetailViewController alloc] initWithOrderId:[NSString stringWithFormat:@"%@",model.OrderNO]];
        
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    [_scrollView addSubview:_all];
    
    _obligation = [[ObligationTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 94)];
    _obligation.cellDidSelected = ^(NSString *status, NSInteger sindex, NSInteger rindex){
        AllOrderModel *model = [weakSelf.obligation.obligationArray objectAtIndex:sindex];
        
        OrderDetailViewController *detail = [[OrderDetailViewController alloc] initWithOrderId:[NSString stringWithFormat:@"%@",model.OrderNO]];
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    [_scrollView addSubview:_obligation];
    
    _committed = [[CommittedTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 94)];
    _committed.cellDidSelected = ^(NSString *status, NSInteger sindex, NSInteger rindex){
        AllOrderModel *model = [weakSelf.committed.committedArray objectAtIndex:sindex];
        
        OrderDetailViewController *detail = [[OrderDetailViewController alloc] initWithOrderId:[NSString stringWithFormat:@"%@",model.OrderNO]];
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    [_scrollView addSubview:_committed];
    
    _terminalReceiving = [[TerminalReceivingTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 94)];
    _terminalReceiving.cellDidSelected = ^(NSString *status, NSInteger sindex, NSInteger rindex){
        AllOrderModel *model = [weakSelf.terminalReceiving.terminalArray objectAtIndex:sindex];
        
        OrderDetailViewController *detail = [[OrderDetailViewController alloc] initWithOrderId:[NSString stringWithFormat:@"%@",model.OrderNO]];
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    [_scrollView addSubview:_terminalReceiving];
    
    _evaluation = [[EvaluationTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 4, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 94)];
    _evaluation.cellDidSelected = ^(NSString *status, NSInteger sindex, NSInteger rindex){
        AllOrderModel *model = [weakSelf.evaluation.evaluationArray objectAtIndex:sindex];
        
        OrderDetailViewController *detail = [[OrderDetailViewController alloc] initWithOrderId:[NSString stringWithFormat:@"%@",model.OrderNO]];
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    [_scrollView addSubview:_evaluation];
}


#pragma mark === 设置显示页面 ===
- (void)setSlider{
    for (UILabel *label in _labelArray) {
        label.textColor = [UIColor blackColor];
    }
    UILabel *label = (id)[self.view viewWithTag:11110 + 10 + _index];
    label.textColor = APPCOLOR;
    _slider.frame = CGRectMake(8 + (SCREEN_WIDTH / 5) * _index, height - 3, SCREEN_WIDTH / 5 - 16, 3);
    _scrollView.contentOffset = CGPointMake(_index * SCREEN_WIDTH, 0);
}

#pragma mark === 导航栏Item ===
- (void)search{
    NSLog(@"搜索");
}
- (void)more{
    NSLog(@"更多");
}

#pragma mark === 网络请求 ===
- (void)network:(NSInteger)status
{
    NSDictionary *paraments = @{
                                @"userId"  : _userId
                                };
    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"7494/api/Order/GetOrderSkuByUserId?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"订单--%ld : %@",(long)status, object);
        
        if (!_allOrderArray) {
            _allOrderArray = [NSMutableArray array];
        }else [_allOrderArray removeAllObjects];
        
        if (!_obligationArray) {
            _obligationArray = [NSMutableArray array];
        }else [_obligationArray removeAllObjects];
        
        if (!_committedArray) {
            _committedArray = [NSMutableArray array];
        }else [_obligationArray removeAllObjects];
        
        if (!_terminalReceivingArray) {
            _terminalReceivingArray = [NSMutableArray array];
        }else [_terminalReceivingArray removeAllObjects];
        
        if (!_evaluationArray) {
            _evaluationArray = [NSMutableArray array];
        }else [_evaluationArray removeAllObjects];
        
        
        for (NSDictionary *dic in object[@"Rows"]) {
            
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *diccc in dic[@"OMSOrderSkuList"]) {
                AllOrderModel *model = [AllOrderModel parseUserWithDictionary:diccc];
                [tempArray addObject:model];
            }
            
            NSMutableString *countt = [NSMutableString stringWithFormat:@"%@", dic[@"TotalPrice"]];
            NSRange rangee = [countt rangeOfString:@"."];
            if (rangee.location == NSNotFound) {
                [countt appendString:@".00"];
            }else if (rangee.length == 1){
                [countt appendString:@"0"];
                countt = [NSMutableString stringWithFormat:@"%.2f", [dic[@"TotalPrice"] floatValue]];
            }
            
            NSArray *totalArray = @[[NSMutableString stringWithFormat:@"%@", dic[@"TotalCount"]], countt];
            [_allOrderArray addObject:@[tempArray, totalArray]];
        }
        switch (status) {
            case 0:
            {
                _all.allOrderArray = _allOrderArray;
                [_all.tableView reloadData];
            
            }
                break;
            case 1:
            {
//                  _obligation.obligationArray= _allOrderArray;
//                  [_obligation.obligationTableView reloadData];
              
            }
                break;
            case 2:
            {
//                  _committed.committedArray= _allOrderArray;
//                  [_committed.commTableView reloadData];
                
            }
                break;
            case 3:
            {
//                   _terminalReceiving.terminalArray= _allOrderArray;
//                   [_terminalReceiving.terminalTableView reloadData];
               
            }
                break;
            case 4:
            {
//                    _evaluation.evaluationArray= _allOrderArray;
//                    [_evaluation.evaluationTableView reloadData];
                
            }
                break;
                
            default:
                break;
        }
  
        
     
      
       
       
        
   
    } withFailureBlock:^(NSError *error) {
        NSLog(@"error : %@", error);
    } progress:nil];
}
@end
